package com.frinder.demo2;

import akka.actor.ActorRef;
import akka.actor.Terminated;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import scala.concurrent.Future;

/**
 * Created by frinder on 2017/7/24.
 */
public class WatchActor extends UntypedActor {

    LoggingAdapter logger = Logging.getLogger(getContext().system(), this);

    public WatchActor(ActorRef actorRef) {
        getContext().watch(actorRef);
    }

    @Override
    public void onReceive(Object o) throws Exception {
        if (o instanceof Terminated) {
            logger.info("{} has Terminated. now shutdown the system.", ((Terminated) o).getActor().path());
            Future<Terminated> future = getContext().system().terminate();
            logger.info("{}", future.value());
        } else {
            unhandled(o);
        }
    }
}
